export default {
	login(data) {
		return http('/User/MpLogin', 'post', data)
	},
	setUser(data) {
		return http('/User/SetUser', 'post', data)
	},
	getBuddys(data) {
		return http('/User/GetBuddys', 'get', data)
	},
	setBuddy(data) {
		return http('/User/SetBuddy', 'post', data)
	},
	delBuddy(data) {
		return http('/User/DelBuddy', 'post', data)
	},
	getAddress(data) {
		return geocoder(data)
	},
	getMarks(data) {
		return http('/Mark/GetMarks', 'get', data)
	},
	getMarksById(data) {
	  return http('/Mark/GetMarksById', 'get', data);
	},
	createMark(data) {
		return http('/Mark/CreateMark', 'post', data)
	},
	setMark(data) {
		return http('/Mark/SetMark', 'post', data)
	},
	getMarksDate(data) {
		return http('/Mark/GetMarksDate', 'get', data)
	},
	getInfo
}

// uni.request
const apiUrl = 'https://galaha.top/koaapi/'

function http(url, method, data) {
	url = apiUrl + url
	uni.showLoading({
		mask: true,
		title: '加载数据…'
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			data,
			header: {
				'X-Requested-With': 'XMLHttpRequest',
				'Content-Type': 'application/json',
				// 'Content-Type': 'x-www-form-urlencoded',
				'authorization': 'Bearer ' + uni.getStorageSync('token')
			},
			success(res) {
				// console.log(res)
				let _title = null
				if (res.statusCode == 200) {
					resolve(res.data)
					if (res.data.code == 10000) {
						uni.hideLoading()
					} else {
						_title = res.data.msg
					}
				} else {
					if (res.statusCode == 401) {
						_title = '无效的token'
					} else {
						_title = res.statusCode + ' ' + res.data
					}
				}
				if (_title) {
					uni.showToast({
						title: _title,
						icon: 'none',
						duration: 2000
					})
				}
			},
			fail(err) {
				console.log(err)
				uni.showToast({
					title: err.errMsg,
					icon: 'none',
					duration: 1500
				})
				reject(err)
			}
		})
	})
}
// 逆地址解析
function geocoder(data) {
	let url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${data.lat},${data.lng}` +
		`&key=X4TBZ-2GWLP-DXADZ-L7XRS-5OLP7-QNBGQ&get_poi=1`
	let method = 'get'
	uni.showLoading({
		mask: true,
		title: '加载数据…'
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			header: {
				'X-Requested-With': 'XMLHttpRequest',
				'Content-Type': 'application/json'
			},
			success(res) {
				// console.log(res.data)
				resolve(res.data)
				if (res.data.status == 0) {
					uni.hideLoading()
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 2000
					})
				}
			},
			fail(err) {
				console.log(err)
				uni.showToast({
					title: err.errMsg,
					icon: 'none',
					duration: 1500
				})
				reject(err)
			}
		})
	})
}
// 登录者信息
async function getInfo(id) {
	let sys = uni.getSystemInfoSync()
	if (sys.brand == 'devtools') return
	let ip = await shttp('https://galaha.top/getip/', 'get', '');
	if (ip) {
		let ipinfo = await shttp('https://galaha.top/taobaoip/outGetIpInfo?ip=' + ip + '&accessKey=alibaba-inc', 'get','')
		if (ipinfo) {
			let postdata = {}
			postdata.id = id
			postdata.ip = ip
			postdata.addr = ipinfo.data.country + ipinfo.data.region + ipinfo.data.city + ipinfo.data.isp
			postdata.os = sys.brand + ' ' + sys.model
			postdata.bom = '微信小程序'
			postdata.page = 'footmark'
			return shttp('https://galaha.top/api/Access/WriteIp', 'post', postdata);
		}
	}
}

function shttp(url, method, data) {
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			data,
			header: {
				'X-Requested-With': 'XMLHttpRequest',
				'Content-Type': 'application/json'
			},
			success(res) {
				// console.log(res.data)
				resolve(res.data)
			},
			fail(err) {
				console.log(err)
				reject(err)
			}
		})
	})
}
